//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.ruoyi.web.core.config;

import com.ruoyi.cust.domain.CityProject;
import com.ruoyi.web.core.utils.UserRecentSearch;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.time.Instant;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Component
public class SearchRedisHelper {
    public static final String        HOT_SEARCH             = "product_hot_search";
    public static final String        RECENT_SEARCH          = "product_recent_search";
    public static final Integer       CURRENT_SEARCH_SIZE    = 3;
    public static final Integer       HOT_SEARCH_EXPIRE_TIME = 3;
    @Resource
    private             RedisTemplate redisTemplate;

    public SearchRedisHelper() {
    }

    @PostConstruct
    public void setHotSearchExpireTime() {
        this.redisTemplate.expire("product_hot_search", (long) HOT_SEARCH_EXPIRE_TIME, TimeUnit.SECONDS);
    }

    public void addRedisRecentSearch(UserRecentSearch userRecentSearch) {
        ZSetOperations<String, UserRecentSearch> zSet = this.redisTemplate.opsForZSet();
        String key = "product_recent_search_" + userRecentSearch.getSearchInfo();
        zSet.add(key, userRecentSearch, (double) Instant.now().getEpochSecond());
        Set<ZSetOperations.TypedTuple<UserRecentSearch>> typedTuples        = zSet.reverseRangeWithScores(key, 0L, -1L);
        Set<UserRecentSearch>                            userRecentSearches = this.listRecentSearch(userRecentSearch);
        System.out.println("userRecentSearches:" + userRecentSearches);
        if (userRecentSearches.size() > CURRENT_SEARCH_SIZE) {
            UserRecentSearch userRecentSearchLast = userRecentSearches.stream().reduce((first, second) -> second).orElse(null);
            zSet.remove(key, userRecentSearchLast);
        }

    }

    public Set<CityProject> listHotSearch(String storeType) {
        return this.redisTemplate.opsForZSet().reverseRangeWithScores("product_hot_search_"+storeType, 0L, 5L);
    }

    public void addRedisHotSearch(List<CityProject> productList,String storeType) {
        productList.forEach((cityProject) -> this.redisTemplate.opsForZSet().incrementScore("product_hot_search_"+storeType, cityProject, 1.0F));
    }

    public Set<UserRecentSearch> listRecentSearch(UserRecentSearch userRecentSearch) {
        Set<ZSetOperations.TypedTuple<UserRecentSearch>> typedTuples = this.redisTemplate.opsForZSet().reverseRangeWithScores("product_recent_search", 0L, -1L);
        return (Set) Optional.ofNullable(typedTuples).map((tuples) -> (LinkedHashSet) tuples.stream().map(ZSetOperations.TypedTuple::getValue).filter(Objects::nonNull).filter((userRecentSearchDB) -> Objects.equals(userRecentSearch.getUnionId(), userRecentSearch.getUserId())).collect(Collectors.collectingAndThen(Collectors.toCollection(LinkedHashSet::new), LinkedHashSet::new))).orElseGet(LinkedHashSet::new);
    }
}
